﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Weapp.Client;
using Weapp.Client.Models;
using Weapp.Data;

namespace Weapp
{
    public partial class FormSizeCategoryAbm : Form
    {
        #region PrivateMembers

        private readonly IWeappWebServiceClient _client;
        DataSet ds;
        private bool edit;
        #endregion

        #region Constructors
        public FormSizeCategoryAbm(WeappContext ctx)
        {
            InitializeComponent();
            _client = new WeappWebServiceClient();
        }
        #endregion

        #region Methods
        public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
        {
            DataTable dtReturn = new DataTable();

            // column names 
            PropertyInfo[] oProps = null;

            if (varlist == null) return dtReturn;

            foreach (T rec in varlist)
            {
                // Use reflection to get property names, to create table, Only first time, others will follow 
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    foreach (PropertyInfo pi in oProps)
                    {
                        var colType = pi.PropertyType;

                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                        == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }

                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }

                DataRow dr = dtReturn.NewRow();

                foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) ?? DBNull.Value;
                }

                dtReturn.Rows.Add(dr);
            }
            return dtReturn;
        }
        private void EnableFields(bool pbEnabled)
        {
            textFieldSizeCategoryName.Enabled = pbEnabled;
            numericUpDownFieldAttackAndArmorClassModifier.Enabled = pbEnabled;
            numericUpDownFieldBipedCarryingCapacityMultiplier.Enabled = pbEnabled;
            numericUpDownFieldMinHeightOrLength.Enabled = pbEnabled;
            numericUpDownFieldMaxHeightOrLength.Enabled = pbEnabled;
            numericUpDownFieldNaturalReachLong.Enabled = pbEnabled;
            numericUpDownFieldNaturalReachTall.Enabled = pbEnabled;
            numericUpDownFieldQuadrupedCarryingCapacityMultiplier.Enabled = pbEnabled;
            numericUpDownFieldSpace.Enabled = pbEnabled;
            numericUpDownFieldSpecialAttacksModifier.Enabled = pbEnabled;
            numericUpDownFieldMinWeight.Enabled = pbEnabled;
            numericUpDownFieldMaxWeight.Enabled = pbEnabled;
        }
        private void ClearFields()
        {
            textFieldSizeCategoryName.FieldValue = "";
            numericUpDownFieldAttackAndArmorClassModifier.FieldValue = 0;
            numericUpDownFieldBipedCarryingCapacityMultiplier.FieldValue = 0;
            numericUpDownFieldMinHeightOrLength.FieldValue = 0;
            numericUpDownFieldMaxHeightOrLength.FieldValue = 0;
            numericUpDownFieldNaturalReachLong.FieldValue = 0;
            numericUpDownFieldNaturalReachTall.FieldValue = 0;
            numericUpDownFieldQuadrupedCarryingCapacityMultiplier.FieldValue = 0;
            numericUpDownFieldSpace.FieldValue = 0;
            numericUpDownFieldSpecialAttacksModifier.FieldValue = 0;
            numericUpDownFieldMinWeight.FieldValue = 0;
            numericUpDownFieldMaxWeight.FieldValue = 0;
        }
        private void UpdateDataGridView()
        {
            ds = new DataSet();
            ds.Tables.Add(LINQToDataTable(_client.GetAllSizeCategories().AsEnumerable()));
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Refresh();
        }
        #endregion

        #region Events
        private void NewSizeCategoryForm_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = true;
            ds = new DataSet();
            ds.Tables.Add((LINQToDataTable((_client.GetAllSizeCategories()))));
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.ClearSelection();
            ButtonEdit.Enabled = false;
            EnableFields(false);
            edit = false;
            ClearFields();
        }
        private void ButtonNew_Click(object sender, EventArgs e)
        {
            ClearFields();
            EnableFields(true);
            ButtonNew.Visible = false;
            ButtonNew.Enabled = false;
            ButtonEdit.Visible = false;
            ButtonEdit.Enabled = false;
            ButtonSave.Visible = true;
            ButtonSave.Enabled = true;
            ButtonCancel.Visible = true;
            ButtonCancel.Enabled = true;
            dataGridView1.ClearSelection();
            dataGridView1.Enabled = false;
        }
        private void ButtonSave_Click(object sender, EventArgs e)
        {
            SizeCategoryModel sc = new SizeCategoryModel();
            sc.AttackAndArmorClassModifier = Convert.ToInt32(numericUpDownFieldAttackAndArmorClassModifier.FieldValue);
            sc.BipedCarryingCapacityMultiplier = Convert.ToInt32(numericUpDownFieldBipedCarryingCapacityMultiplier.FieldValue);
            sc.MinHeightOrLength = Convert.ToInt32(numericUpDownFieldMinHeightOrLength.FieldValue);
            sc.Name = textFieldSizeCategoryName.FieldValue;
            sc.NaturalReachLong = numericUpDownFieldNaturalReachLong.FieldValue;
            sc.NaturalReachTall = numericUpDownFieldNaturalReachTall.FieldValue;
            sc.QuadrupedCarryingCapacityMultiplier = Convert.ToInt32(numericUpDownFieldQuadrupedCarryingCapacityMultiplier.FieldValue);
            sc.Space = Convert.ToInt32(numericUpDownFieldSpace.FieldValue);
            sc.SpecialAttacksModifier = Convert.ToInt32(numericUpDownFieldSpecialAttacksModifier.FieldValue);
            sc.MinWeight = Convert.ToInt32(numericUpDownFieldMinWeight.FieldValue);

            if (edit)
            {
                sc.SizeCategoryId = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["SizeCategoryId"].Value);
                _client.UpdateSizeCategory(sc);
            }
            else
            {
                _client.NewSizeCategory(sc);
            }

            EnableFields(false);
            ButtonNew.Visible = true;
            ButtonNew.Enabled = true;
            ButtonEdit.Visible = true;
            ButtonEdit.Enabled = false;
            ButtonSave.Visible = false;
            ButtonSave.Enabled = false;
            ButtonCancel.Visible = false;
            ButtonCancel.Enabled = false;
            dataGridView1.Enabled = true;
            dataGridView1.ClearSelection();
            UpdateDataGridView();
        }
        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            ClearFields();
            EnableFields(false);
            ButtonNew.Visible = true;
            ButtonNew.Enabled = true;
            ButtonEdit.Visible = true;
            ButtonEdit.Enabled = false;
            ButtonSave.Visible = false;
            ButtonSave.Enabled = false;
            ButtonCancel.Visible = false;
            ButtonCancel.Enabled = false;
            dataGridView1.Enabled = true;
            dataGridView1.ClearSelection();
            edit = false;
        }
        private void ButtonEdit_Click(object sender, EventArgs e)
        {
            edit = true;
            EnableFields(true);
            ButtonNew.Visible = false;
            ButtonNew.Enabled = false;
            ButtonEdit.Visible = false;
            ButtonEdit.Enabled = false;
            ButtonSave.Visible = true;
            ButtonSave.Enabled = true;
            ButtonCancel.Visible = true;
            ButtonCancel.Enabled = true;
            dataGridView1.Enabled = false;
        }
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                SizeCategoryModel sc = _client.GetSizeCategory((int)dataGridView1.SelectedRows[0].Cells["SizeCategoryId"].Value);

                textFieldSizeCategoryName.FieldValue = sc.Name;
                numericUpDownFieldAttackAndArmorClassModifier.FieldValue = sc.AttackAndArmorClassModifier;
                numericUpDownFieldBipedCarryingCapacityMultiplier.FieldValue = sc.BipedCarryingCapacityMultiplier;
                numericUpDownFieldMinHeightOrLength.FieldValue = sc.MinHeightOrLength;
                numericUpDownFieldMaxHeightOrLength.FieldValue = sc.MaxHeightOrLength;
                numericUpDownFieldNaturalReachLong.FieldValue = sc.NaturalReachLong;
                numericUpDownFieldNaturalReachTall.FieldValue = sc.NaturalReachTall;
                numericUpDownFieldQuadrupedCarryingCapacityMultiplier.FieldValue = sc.QuadrupedCarryingCapacityMultiplier;
                numericUpDownFieldSpace.FieldValue = sc.Space;
                numericUpDownFieldSpecialAttacksModifier.FieldValue = sc.AttackAndArmorClassModifier;
                numericUpDownFieldMinWeight.FieldValue = sc.MinWeight;
                numericUpDownFieldMaxWeight.FieldValue = sc.MaxWeight;

                ButtonEdit.Enabled = true;
            }
        }
        #endregion
        
    }
}
